/*
 * Copyright 2020 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.animation;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;

import com.mikepenz.iconics.IconicsBrush;
import com.mikepenz.iconics.utils.KtExtensions;

/**
 * @author pa.gulko zTrap (30.11.2018)
 */
public class BlinkScaleProcessor extends IconicsAnimationProcessor {
    private static final long DEFAULT_DURATION = 500L;
    private static final int MAX = 100;
    private static final float HALF = 2f;
    private float minimumScale = 0f;
    private float maximumScale = 1f;

    /**
     * 默认构造
     */
    public BlinkScaleProcessor() {
        setDuration(DEFAULT_DURATION);
        setAnimationTag("blink_scale");
        setRepeatMode(ValueAnimator.RepeatMode.REVERSE);
    }

    /**
     * 设置最小缩放
     *
     * @param minimumScale 最小缩放
     */
    public void setMinimumScale(float minimumScale) {
        this.minimumScale = minimumScale;
    }

    /**
     * 设置最大缩放
     *
     * @param maximumScale 最大缩放
     */
    public void setMaximumScale(float maximumScale) {
        this.maximumScale = maximumScale;
    }

    @Override
    public void processorPreDraw(Canvas canvas, IconicsBrush<Paint> iconBrush, IconicsBrush<Paint> iconContourBrush,
                                 IconicsBrush<Paint> backgroundBrush, IconicsBrush<Paint> backgroundContourBrush) {
        float scaleByPercent = (maximumScale - minimumScale) / MAX;
        float scale = getAnimatedPercent() * scaleByPercent;
        Rect bounds = getDrawableBounds();
        canvas.save();
        canvas.scale(scale, scale, bounds.getWidth() / HALF, bounds.getHeight() / HALF);
    }

    @Override
    public void processPostDraw(Canvas canvas) {
        canvas.restore();
    }
}
